package com.abel.service.helper;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.abel.manager.exception.EmailSendException;

public class EmailSender {

	protected final Log logger = LogFactory.getLog(getClass());
	
	private String smtpHost;
	
	public void sendEmail(String from, String to, String subject, String message) 
		throws UnknownHostException, EmailSendException {

		List<BodyPart> bodyParts = new ArrayList<BodyPart>();
		
		// text content
		bodyParts.add(createBodyPart(message, "text/html"));
		
		//setting stmp properties
		
		if (smtpHost == null) {
			
			throw new UnknownHostException("SMTP server not set. Set the smtpHost before invoking this method");
		}
		Properties props = System.getProperties();
		props.put("mail.smtp.host", this.smtpHost);
		
		Session session = Session.getInstance(props, null);
		Message email = new MimeMessage(session);

		InternetAddress intAddressFrom = null;
		InternetAddress[] intAddressTo = null;
		String emailSubject = null;
		
		try {
			intAddressFrom = new InternetAddress(from);
			intAddressTo = InternetAddress.parse(to, false);

			// adding to, from, subject
			email.setFrom(intAddressFrom);
			email.setRecipients(Message.RecipientType.TO, intAddressTo);
			email.setSubject(subject);
			
			// adding body parts(content) to the main body
			MimeMultipart multipart = new MimeMultipart("alternative");
			
			for (BodyPart part : bodyParts) {

				multipart.addBodyPart(part);
			}
			email.setContent(multipart);
			
			// sending email
			Transport.send(email);
			logger.debug("@EmailSender : sendEmail : sent email to  - " + to);
			
		} catch (AddressException e) {
			
			e.printStackTrace();
			String msg = "Can't send email from:" + intAddressFrom + " to:" + intAddressTo + " subject:" + emailSubject;
			logger.debug("@EmailSender : sendEmail : exception - " + msg + " - "  + e);
			throw new EmailSendException(msg, e);
			
		} catch (MessagingException e) {
			
			e.printStackTrace();
			String msg = "Can't send email from:" + intAddressFrom + " to:" + intAddressTo + " subject:" + emailSubject;
			logger.debug("@EmailSender : sendEmail : exception - " + msg + " - "  + e);
			throw new EmailSendException(msg, e);
		}
	}
	
	
	/**
	 * @return the smtpHost
	 */
	public String getSmtpHost() {
		return smtpHost;
	}


	/**
	 * @param smtpHost the smtpHost to set
	 */
	public void setSmtpHost(String smtpHost) {
		this.smtpHost = smtpHost;
	}


	private BodyPart createBodyPart(String content, String type)	{
			
		BodyPart contentBody = new MimeBodyPart();
		
		try {
			contentBody.setContent(content, type);
		}
		catch (MessagingException e) {

			String msg = "Can't add html content to email";
			throw new RuntimeException(msg, e);
		}
		return contentBody;
	}
}
